﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Eaktest;
using SystemCore;

namespace EakTest
{
    public class ControlUtility
    {
        #region Enabled

        public static void EnabledControl(bool bEnabled, params Control[] Controls)
        {
            foreach (Control ctl in Controls)
            {
                if (ctl == null)
                    return;

                if (ctl is System.Windows.Forms.TextBox)
                {
                    TextBox txt = ((System.Windows.Forms.TextBox)ctl);

                    txt.ReadOnly = !bEnabled;
                    txt.TabStop = bEnabled;

                    if (bEnabled)
                    {
                        txt.BackColor = UserContext.COLOR_NORMAL_BG;
                        txt.ForeColor = UserContext.COLOR_NORMAL_FG;
                    }
                    else
                    {
                        txt.BackColor = UserContext.COLOR_READONLY_BG;
                        txt.ForeColor = UserContext.COLOR_READONLY_FG;
                    }
                }
                else if (ctl is System.Windows.Forms.ComboBox)
                {
                    ComboBox cbo = ((System.Windows.Forms.ComboBox)ctl);
      
                    cbo.TabStop = bEnabled;
                    cbo.Enabled = bEnabled;

                    if (bEnabled)
                    {
                        cbo.BackColor = UserContext.COLOR_NORMAL_BG;
                        cbo.ForeColor = UserContext.COLOR_NORMAL_FG;
                    }
                    else
                    {
                        cbo.BackColor = UserContext.COLOR_READONLY_BG;
                        cbo.ForeColor = UserContext.COLOR_READONLY_FG;
                    }
                }
                else if (ctl is System.Windows.Forms.CheckBox)
                {
                    CheckBox chk = ((System.Windows.Forms.CheckBox)ctl);

                    chk.TabStop = bEnabled;
                    chk.Enabled = bEnabled;

                    if (bEnabled)
                    {
                        chk.BackColor = UserContext.COLOR_NORMAL_BG;
                        chk.ForeColor = UserContext.COLOR_NORMAL_FG;
                    }
                    else
                    {
                        chk.BackColor = UserContext.COLOR_READONLY_BG;
                        chk.ForeColor = UserContext.COLOR_READONLY_FG;
                    }
                }
                else
                {
                    ctl.Enabled = bEnabled;
                }
            }
        }

        public static void EnabledControl(bool bEnabled, params ToolStripButton[] tsButtons)
        {
            foreach (ToolStripButton tsButton in tsButtons)
            {
                if (tsButton != null)
                {
                    tsButton.Enabled = bEnabled;
                }
            }
        }
        
        #endregion

        #region Clear

        public static void ClearControlData(params Control[] Controls)
        {
            foreach (Control ctl in Controls)
            {
                if (ctl == null)
                    return;

                if (ctl is ComboBox)
                {
                    ((ComboBox) ctl).SelectedIndex = -1;
                    ((ComboBox) ctl).Text = string.Empty;
                }
                else if (ctl is TextBox)
                {
                    ((TextBox) ctl).Text = string.Empty;
                }
                else if (ctl is CheckBox)
                {
                    ((CheckBox) ctl).Checked = false;
                }
            }
        }

        #endregion
    }
}
